<?php

namespace app\api\model\service;

use fast\Random;
use think\Exception;
use think\Model;
class AddOrder extends Model
{
    // 表名
    protected $name = 'service_add_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function createOrder($params)
    {
        $order = \app\api\model\service\Order::where(['id'=>$params['order_id'],'user_id'=>$params['user_id'],'status'=>['in',[2,3,4,5]]])->field('id,actendtime,skill_id,shop_id')->find();
        if(!$order)
        {
            throw new Exception('当前订单暂时无法新增附加项目');
        }
        $params['orderId'] = 'GoodsAdd'.Random::alnum(4).'-'.$params['user_id'].'-'.time();
        $addOrder = new AddOrder($params);
        $addOrder->allowField(true)->save();
        $addIds = explode(',',$params['add_ids']);
        $payprice = 0;
        $totalCostSeconds = 0;

        foreach ($addIds as $val)
        {
            $addInfo = explode('-',$val);
            $info = GoodsAdd::where(['id'=>$addInfo[0],'state'=>1])->field('id,name,price,cost_seconds')->find();
            if(!$info){
                continue;
            }
            $payprice+=bcmul($info['price'],$addInfo[1],2);
            $totalCostSeconds+=intval($info['cost_seconds']*$addInfo[1]);
            AddOrderDetail::create(['user_id'=>$params['user_id'],'service_add_order_id'=>$addOrder->id,'price'=>$info['price'],'order_id'=>$params['order_id'],'goods_add_id'=>$addOrder->id,'num'=>$addInfo[1],'cost_seconds'=>$info['cost_seconds'],'name'=>$info['name']]);
        }

        $userMoney = User::getMoney($params['user_id']);

        $endTime = $order['actendtime']+$totalCostSeconds*60;
        $exist = \app\api\model\service\SkillTime::where(['skill_id'=>$order['skill_id'],'state'=>['>',0],'starttime'=>['between',[$order['actendtime'],$endTime]]])->value('id');
        if($exist)
        {
            throw new Exception('当前服务者已被预约,暂时无法接单');
        }

        if($params['paytype'] == 4 && $userMoney<$payprice)
        {
            throw new Exception('余额不足,请及时充值');
        }

        $addOrder->save(['payprice'=>$payprice,'total_cost_seconds'=>$totalCostSeconds],['id'=>$addOrder->id]);
        $data = [];
        $data['paystatus'] = 0;

        if($params['paytype'] != 4)
        {
            $data['pay'] = \addons\service\library\Pay::payOrder(['amount'=>$payprice,'orderid'=>$params['orderId'],'title'=>'支付附加项目费用'],$params['paytype'],$params['user_id'],0);
        }else{
            User::money(-$payprice,$params['user_id'],'支付附加项目费用');
            $addOrder->save(['state'=>1,'paytime'=>time()],['id'=>$addOrder->id]);
            self::finishPay($addOrder->id);
            $data['paystatus'] = 1;
            $data['pay'] = '';
        }
        return $data;
    }

    public static function finishPay($id)
    {
        $addOrder = self::where(['id'=>$id,'state'=>1])->find();
        if(!$addOrder)
        {
            return false;
        }

        $order = Order::where(['id'=>$addOrder['order_id']])->field('id,payprice,add_price,settle_price,skill_id,actendtime,endtime')->find();
        $payprice = bcadd($order['payprice'],$addOrder['payprice'],2);
        $addPrice = bcadd($order['add_price'],$addOrder['payprice'],2);
        $settlePrice = bcadd($order['settle_price'],$addOrder['payprice'],2);
        $endTime = $order['endtime']+$addOrder['total_cost_seconds']*60;
        $actEndTime = $order['actendtime']+$addOrder['total_cost_seconds']*60;
        SkillTime::updateSkillTime(['skill_id'=>$order['skill_id'],'starttime'=>$order['actendtime'],'actendtime'=>$actEndTime],1);
        Order::where(['id'=>$addOrder['order_id']])->update(['payprice'=>$payprice,'add_price'=>$addPrice,'settle_price'=>$settlePrice,'endtime'=>$endTime,'actendtime'=>$actEndTime,'updatetime'=>time()]);
        OrderLog::create(['order_id'=>$addOrder['order_id'],'user_id'=>$addOrder['user_id'],'type'=>13,'content'=>'订单已添加附加项目']);
        return true;
    }

    public static function getOrderDetailList($order_id)
    {
        $addList = self::where(['order_id'=>$order_id,'state'=>1])->column('id');
        $list = [];
        if($addList)
        {
            foreach ($addList as $value)
            {
                $detail = AddOrderDetail::where(['service_add_order_id'=>$value])->select();
                $list = array_merge($list,$detail);
            }
        }
        return $list;
    }

}
